Skip to content

Assertions return boolean #2586

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

adiSuper94
Copy link
Contributor

@adiSuper94 adiSuper94 commented Sep 19, 2020

Fixes #2455. Based on #2564.

@adiSuper94
Copy link
Contributor Author

@novemberborn I have edited test cases only for pass(), fail(), is() and not().
If this is fine, I'll go ahead and edit the testcases for other assertions and edit the type definitions also.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adiSuper94 thanks for redoing the PR.

I've pushed two commits, one for some weird code I spotted, and then the other to return false if the messages are illegal.

I think notThrowsAsync() should also return a boolean, just because we can. It's only the throws() and throwsAsync() assertions that return the thrown error or undefined (which we haven't typed because it's annoying).

With regards to the tests, I appreciate that the assertion tests are rather complicated. I'm struggling to understand the changes you've made though. Could you elaborate on your approach? For these tests too I'd prefer if we could use tap's assertions to check whether an AVA assertion returned true or false.

We do have some setup in place now to use AVA to test AVA, but migrating the assertion tests to that should be separate.

@sindresorhus
Copy link
Member

@adiSuper94 Bump :)

@adiSuper94
Copy link
Contributor Author

@sindresorhus I have been meaning to work on this for the past few weekends. Thanks for the reminder.

@adiSuper94 adiSuper94 force-pushed the feat/assertions-return-bool branch from b403d51 to 2081a9e Compare October 17, 2020 16:03
@adiSuper94 adiSuper94 force-pushed the feat/assertions-return-bool branch from 2081a9e to 3d3a40e Compare October 17, 2020 17:30
@adiSuper94
Copy link
Contributor Author

adiSuper94 commented Oct 17, 2020

@novemberborn , sorry for the delay in response.

I've pushed two commits, one for some weird code I spotted, and then the other to return false if the messages are illegal.

Thanks, I guess my editor is acting a little crazy.

I think notThrowsAsync() should also return a boolean, just because we can. It's only the throws() and throwsAsync() assertions that return the thrown error or undefined (which we haven't typed because it's annoying).

And added returns for notThrowsAsync()

With regards to the tests, I appreciate that the assertion tests are rather complicated. I'm struggling to understand the changes you've made though. Could you elaborate on your approach? For these tests too I'd prefer if we could use tap's assertions to check whether an AVA assertion returned true or false.

We do have some setup in place now to use AVA to test AVA, but migrating the assertion tests to that should be separate.

The approach I took was rather crude. I have changed it. Let me know if it clear now.
I have edited the testcases to not directly use ava.

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @adiSuper94. Thanks for coming back to this.

I've added two suggestions to make the tests actually look for false and true values, rather than falsy/truthy. What do you think?

Would you mind updating the type definitions as well?

@novemberborn
Copy link
Member

You might be able to cherry-pick the type definitions from #2601.

@adiSuper94
Copy link
Contributor Author

adiSuper94 commented Oct 18, 2020

@novemberborn , Sure.

@adiSuper94
Copy link
Contributor Author

adiSuper94 commented Oct 20, 2020

@novemberborn

I've added two suggestions to make the tests actually look for false and true values, rather than falsy/truthy. What do you think?

I accepted those, suggestions. It actually helped me improve the tests. Thanks!

Would you mind updating the type definitions as well?

cherry-picked the type definitions from #2601

Copy link
Member

@novemberborn novemberborn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @adiSuper94. Couple more questions. And I appreciate these assertion tests are rather cumbersome!

t.pass();
} else {
lastFailure = new Error('Assertion failed');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to assign / override this.

@@ -1047,6 +1057,7 @@ test('.throws()', gather(t => {
assertions.throws(() => {
throw new Error('foo');
});
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this return statement? Same below.

Comment on lines -123 to 132
fn();
if (lastFailure) {
if (fn() === false && lastFailure) {
t.pass();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be something like:

const retval = fn();
if (lastFailure) {
  t.false(retval)
}

And something similar in passes below. Combining the variable check with the return value check makes it hard to know what's being tested.

@@ -433,7 +509,7 @@ export interface CbExecutionContext<Context = unknown> extends ExecutionContext<
end(error?: any): void;
}

export type ImplementationResult = PromiseLike<void> | Subscribable | void;
export type ImplementationResult = PromiseLike<void> | Subscribable | boolean | void;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't change. It's the return type of the function you pass to test().

Base automatically changed from master to main February 7, 2021 15:32
@novemberborn
Copy link
Member

Closing in favor of #2696.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Have assertions return booleans
5 participants